package com.msl.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.base.BaseActivity;
import com.msl.bean.AbUserBean;
import com.msl.tool.ToastUtils;
import com.seuic.misc.Misc;
import java.io.IOException;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {
    private String tag = "LoginActivity";
    @Bind(R.id.tvr)
    TextView tvr;
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.ivu)
    ImageView ivu;
    @Bind(R.id.login_id)
    EditText loginId;
    @Bind(R.id.ivp)
    ImageView ivp;
    @Bind(R.id.login_p)
    EditText loginP;
    @Bind(R.id.cb_rp)
    CheckBox cbRp;
    @Bind(R.id.loginbt)
    Button loginbt;
    @Bind(R.id.configbt)
    Button configbt;
    String cid = "";
    String user = "";
    String pw = "";
    boolean flag = false;

    public static String getVersionName(Context mContext) {
        String versionName= "0.0";
        try {
            versionName = mContext.getPackageManager().
                    getPackageInfo(mContext.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            return versionName;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tvTitle.setText("登录");
        loginId.setText(MslApp.sSharedPreferences.getString(MslApp.userk, ""));
        loginP.setText(MslApp.sSharedPreferences.getString(MslApp.pwk, ""));
        tvr.setText("版本号:v"+getVersionName(this));
//        loginId.setText("0200119344");
//        loginP.setText("0200119344");
//        loginId.setText("test123");
//        loginP.setText("000000");
    }

    @Override
    protected void onResume() {
        super.onResume();
        MslApp.url = MslApp.sSharedPreferences.getString(MslApp.addk, "") + MslApp.address;
    }

    @OnClick({R.id.iv_title_left, R.id.cb_rp, R.id.loginbt, R.id.configbt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.cb_rp:
                flag = !flag;
                break;
            case R.id.loginbt:
                if (MslApp.sSharedPreferences.getString(MslApp.cidk, "").length() == 0) {
                    ToastUtils.showSingleToast(this, "请配置企业编号");
                    return;
                }
                cid = MslApp.sSharedPreferences.getString(MslApp.cidk, "");
                user = loginId.getText().toString();
                pw = loginP.getText().toString();
                if (user.length() == 0) {
                    ToastUtils.showSingleToast(this, "请填写用户名");
                    return;
                }
                if (pw.length() == 0) {
                    ToastUtils.showSingleToast(this, "请填写密码");
                    return;
                }
                waitDialog.show();
                Misc misc = new Misc();
                login(MslApp.url + "?action=loginnew&cid=" + cid + "&userName=" + user + "&password=" + pw + "&sn=" + misc.getSN());
//                login(MslApp.url + "?action=loginnew&cid=" + cid + "&userName=" + user + "&password=" + pw + "&sn=90P8F0944000");
//                login(MslApp.url + "?action=loginnew&cid=" + cid + "&userName=" + user + "&password=" + pw + "&sn=dealerweb");
                break;
            case R.id.configbt:
                Intent intent1 = new Intent();
                intent1.setClass(this, ConfigActivity.class);
                startActivity(intent1);
                break;
        }
    }

    void login(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag, "result=" + result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    } else {
                        Log.e(tag, "获取数据失败了" + response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    AbUserBean ab = gson.fromJson(result, AbUserBean.class);
                    if (ab.getErrcode() == 0) {
                        if (flag) {
                            MslApp.sEditor.putString(MslApp.userk, user);
                            MslApp.sEditor.putString(MslApp.pwk, pw);
                            MslApp.sEditor.commit();
                        }
                        MslApp.sEditor.putInt(MslApp.usertype, ab.getData().getUserType());
                        MslApp.sEditor.putInt(MslApp.storeID, ab.getData().getStoreID());
                        MslApp.sEditor.putInt(MslApp.userID, ab.getData().getUserID());
                        MslApp.sEditor.commit();
                        LoginActivity.this.finish();
                        Intent intent = new Intent();
                        intent.putExtra(MslApp.mainicons, ab.getData());
                        intent.setClass(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtils.showSingleToast(LoginActivity.this, ab.getErrmsg());
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
